package com.sup.controller;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.bcel.internal.generic.NEW;
import com.sup.beans.CustomerIdBean;
import com.sup.common.Json;
import com.sup.common.Message;
import com.sup.model.Customer;
import com.sup.service.CustomerService;


@Controller
@RequestMapping(value="/customer")
public class CustomerController {
	
	@Autowired
	CustomerService customerService;
	
	/**
	 * 
	 * @param customer
	 * @return 插入
	 */
	@RequestMapping(value="/save")
	@ResponseBody
	public String saveCustomer(Customer customer){
		Json json=new Json();
		customerService.saveCustomer(customer);
		json.setMsg("添加客户成功");
		json.setSuccess(true);
		return new Message(json).writeJson();
	}
	
	@RequestMapping(value="/update")
	@ResponseBody
	public String updateCustomer(Customer customer){
		Json json=new Json();
		Customer customer2=customerService.findCustomerById(customer.getId());
		if(customer2.compareTo(customer)){
			customerService.updateCustomer(customer);
			json.setSuccess(true);
			json.setMsg("修改用户成功");
		}else{
			json.setMsg("修改失败");
		}
		return new Message(json).writeJson();
	}
	
	@RequestMapping(value="/delete")
	@ResponseBody
	public String deleteCustomer(Long id){
		Json json=new Json();
		customerService.removeCustomer(id);
		json.setSuccess(true);
		json.setMsg("删除客户成功");
		return new Message(json).writeJson();
	}
	/**
	 * 得到数据列表
	 */
	@RequestMapping(value="/list")
	@ResponseBody
	public String grid(@RequestParam(value="page",defaultValue="1")Integer page,@RequestParam(value="rows")Integer rows){
		int pageIndex=page-1;
		Page<Customer> customerPage=customerService.findCustomers(pageIndex, rows);
		Iterator<Customer> iterator=customerPage.iterator();
		List<Customer> customerList=new ArrayList<Customer>();
		while(iterator.hasNext()){
			customerList.add(iterator.next());
		}
		int total=(int) customerPage.getTotalElements();
		return new Message(total, customerList).toJson();
	}
	
	@RequestMapping(value="/ids")
	@ResponseBody
	public String getCustomerIds(){
		List<Customer> customers=customerService.findAll();
		List<CustomerIdBean> customerIdBeans=new ArrayList<CustomerIdBean>();
		if(customers!=null){
			Iterator<Customer> iterator=customers.iterator();
			while(iterator.hasNext()){
				Customer customer=iterator.next();
				CustomerIdBean customerIdBean=new CustomerIdBean();
				customerIdBean.setId(customer.getId());
				customerIdBean.setCustomerId(customer.getName());
				customerIdBeans.add(customerIdBean);
			}
		}
		return new Message(customerIdBeans).writeJson();
	}

}
